package com.chen.servicemap.remote;

import com.alibaba.fastjson2.JSONObject;
import com.chen.internalcommon.constant.MapConfigConstants;
import com.chen.internalcommon.dto.ResponseResult;
import com.chen.internalcommon.response.ServiceResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author chenLiuPing
 **/
@Service
@Slf4j
public class ServiceClient {

    @Value(("${map.key}"))
    private String key;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult add(String name) {
        // 拼接请求地址url
        StringBuilder url = new StringBuilder(MapConfigConstants.SERVICE_ADD_URL);
        url.append("?key=").append(key);
        url.append("&name=").append(name);
        log.info("url:{}", url);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(),null,  String.class);
        String body = forEntity.getBody();
        JSONObject jsonObject = JSONObject.parseObject(body);
        JSONObject data = jsonObject.getJSONObject("data");
        String sid = data.getString("sid");
        return ResponseResult.success(new ServiceResponse(sid));
    }
}
